package com.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.Product;
import com.service.ProductService;
import com.service.impl.ProductServiceImpl;

@WebServlet("/ProductController")
public class ProductController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ProductController() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*
		 * request.setCharacterEncoding("UTF-8");
		 * response.setCharacterEncoding("UTF-8");
		 * response.setContentType("text/html;charset=UTF-8");
		 */

		String method = request.getParameter("method");

		if ("addProduct".equals(method))
			addProduct(request, response);
		else if ("deleteProduct".equals(method))
			deleteProduct(request, response);
		else if ("updateProduct".equals(method))
			updateProduct(request, response);
		else if ("selectProduct".equals(method))
			selectProduct(request, response);
	}

	// 添加图书
	protected void addProduct(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// 获取页面信息
		String productNo = request.getParameter("productNo");
		String productName = request.getParameter("productName");
		String price = request.getParameter("price");
		String description = request.getParameter("description");
		String state = request.getParameter("state");

		// 添加数据
		Product product = new Product();
		product.setProductNo(productNo);
		product.setProductName(productName);
		product.setPrice(Double.parseDouble(price));
		product.setDescription(description);
		product.setProductType_Id(Integer.parseInt(state));

		ProductService pd = new ProductServiceImpl();
		boolean b = pd.addProduct(product);

		String resultJson = null;

		if (b) {
			resultJson = "{\"success\":true,\"msg\":\"添加成功！\"}";
		} else {
			resultJson = "{\"success\":false,\"msg\":\"添加失败!\"}";
		}
		response.getWriter().print(resultJson);
		response.getWriter().flush();
	}

	// 删除图书
	protected void deleteProduct(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String id = request.getParameter("id");
		String resultJson = null;

		ProductService pd = new ProductServiceImpl();
		boolean b = pd.deleteProduct(Integer.parseInt(id));
		if (b) {
			resultJson = "{\"success\":true,\"msg\":\"删除成功！\"}";
		} else {
			resultJson = "{\"success\":false,\"msg\":\"删除失败!\"}";
		}
		response.getWriter().print(resultJson);
		response.getWriter().flush();
	}

	// 修改图书
	protected void updateProduct(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// 获取页面信息
		String productNo = request.getParameter("productNo");
		String productName = request.getParameter("productName");
		String description = request.getParameter("description");
		String price = request.getParameter("price");
		String id = request.getParameter("id");

		// 添加数据
		Product product = new Product();
		product.setProductNo(productNo);
		product.setProductName(productName);
		product.setPrice(Double.parseDouble(price));
		product.setDescription(description);

		ProductService pd = new ProductServiceImpl();
		boolean b = pd.updateProduct(product, Integer.parseInt(id));

		String resultJson = null;

		if (b) {
			resultJson = "{\"success\":true,\"msg\":\"修改成功！\"}";
		} else {
			resultJson = "{\"success\":false,\"msg\":\"修改失败!\"}";
		}
		response.getWriter().print(resultJson);
		response.getWriter().flush();
	}

	// 查询图书
	protected void selectProduct(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		String key = request.getParameter("key");
		String type = request.getParameter("type");
		String sort = request.getParameter("sort");
		String order = request.getParameter("order");
		int startPage = Integer.parseInt(page);
		int endRows = Integer.parseInt(rows);
		ProductService pd = new ProductServiceImpl();
		response.getWriter().print(
				pd.selectProduct(key, type, startPage, endRows, sort, order));
		response.getWriter().flush();
	}
}
